package org.tostring.pork;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.pork.domain.EmployeesVO;
import org.pork.service.EmployeesService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


@Controller
@RequestMapping("/login")
public class LoginController {
	
		private static final Logger logger = Logger.getLogger(LoginController.class);
		
		@Inject
		EmployeesService ePService;

		
		//JSP 페이지로 화면 이동
		@RequestMapping(method = RequestMethod.GET)
		public String fileUploadForm(HttpSession session) {
			
			if(session.getAttribute("userLoginInfo") != null){
				return "redirect:main";
			}
			
			return "login";
			
		}

	    // 로그아웃
	    @RequestMapping("logout")
	    public String logout(HttpSession session) {
	        session.setAttribute("userLoginInfo", null);
	        return "redirect:login";
	    }
	    
	    // 로그인 처리
	    @RequestMapping(method = RequestMethod.POST)
	    public String  loginProcess(EmployeesVO eVO, HttpSession session, HttpServletRequest request) {
	    	
	    	logger.info("loginProcess................");
	    	
	       //ModelAndView mav = new ModelAndView();
	        
	        logger.info("loginProcess................1");
	        
	      //  mav.setViewName("redirect:login");
	 
	        logger.info("loginProcess................2");
	        
	        EmployeesVO epVO = null;
	        
			try {
				epVO = ePService.view(eVO.getEpid());
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			
			}
	        
	        logger.info("loginProcess................3");
	        
	        
	       if(epVO.getEpid().equals(epVO.getEpid()) && eVO.getPw().equals(eVO.getPw())){
	        	
	    	   	logger.info("loginProcess................4");
	        	session.setAttribute("userLoginInfo", epVO);
	        	 
	        
	        }
	       
	        return "redirect:/main";
	    }
}
